define([
        "require",
        "jQuery",
        "bootstrap",
        "bootstrap-dialog",
        "bootstrapNotify"
    ], function (require, jQuery, bootstrap, dialog, bootstrapNotify) {
        var tools = {};

        /*
         Util
         */
        function extendOptions(options, defaultOptions) {
            var extendOptions = {};
            if (typeof options[0] === 'object' && options[0].constructor === {}.constructor) {
                extendOptions = $.extend(true, {}, defaultOptions, options[0]);
            } else if (typeof options[0] === 'string') {
                extendOptions = $.extend(true, {}, defaultOptions, {
                    message: options[0],
                    callback: typeof options[1] !== 'undefined' ? options[1] : null
                });
            }
            return extendOptions;
        }

        function isEmpty(v) {
            switch (typeof v) {
                case 'undefined':
                    return true;
                case 'string':
                    if (v.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true;
                    break;
                case 'boolean':
                    if (!v) return true;
                    break;
                case 'number':
                    if (isNaN(v)) return true;
                    break;
                case 'object':
                    if (null === v || v.length === 0) return true;
                    for (var i in v) {
                        return false;
                    }
                    return true;
            }
            return false;
        }

        tools.isEmpty = isEmpty;

        /*
         DateRangePicker
         */
        tools.timeFormat = "YYYY/MM/DD HH:mm:ss";
        tools.dateRangePicker = function (selector, options, callback) {
            var defaultOptions = {
                'showDropdowns': false,
                'timePicker24Hour': true,
                'timePickerSeconds': true,
                //'dateLimit': {'years': 3},
                'locale': tools.dateRangePicker.locale,
                'linkedCalendars': false,
                'alwaysShowCalendars': true,
                'opens': "left"
            };
            $(selector).daterangepicker($.extend(true, defaultOptions, options), callback);
        };

        tools.dateRangePicker.locale = {
            "format": tools.timeFormat,
            "separator": " - ",
            "applyLabel": "确认",
            "cancelLabel": "取消",
            "fromLabel": "从",
            "toLabel": "到",
            "customRangeLabel": "自定义",
            "weekLabel": "星期",
            "daysOfWeek": [
                "天",
                "一",
                "二",
                "三",
                "四",
                "五",
                "六"
            ],
            "monthNames": [
                "一月",
                "二月",
                "三月",
                "四月",
                "五月",
                "六月",
                "七月",
                "八月",
                "九月",
                "十月",
                "十一月",
                "十二月"
            ],
            "firstDay": 1
        };

        /*
         Notify
         */
        tools.notice = function (message) {
            $.notify({
                icon: 'glyphicon glyphicon-ok-sign',
                // title: "操作结果:",
                message: message
            }, {
                delay: 5,
                type: "success",
                placement: {
                    from: "top",
                    align: "center"
                },
                offset: 5
            });
        };

        function notifyBase(content, options) {
            var defaultContent = $.extend(true, {}, content);
            var defaultOptions = $.extend(true, {
                autoHide: true,
                delay: 5000,
                // time: 10,
                type: "notify",
                placement: {
                    from: "bottom",
                    align: "right"
                }
            }, options);

            return $.notify(defaultContent, defaultOptions);
        }

        function notifyMiniBase(content, options) {
            var defaultContent = $.extend(true, {}, content);
            var defaultOptions = $.extend(true, {
                template: '<div data-notify="container" class="col-xs-11 col-sm-3 col-md-2 col-lg-1 alert alert-{0}" role="alert">' +
                '<button type="button" aria-hidden="true" class="close" data-notify="dismiss">×</button>' +
                '<span data-notify="icon"></span> ' +
                '<span data-notify="title">{1}</span> ' +
                '<span data-notify="message">{2}</span>' +
                '<div class="progress" data-notify="progressbar">' +
                '<div class="progress-bar progress-bar-{0}" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div>' +
                '</div>' +
                '<a href="{3}" target="{4}" data-notify="url"></a>' +
                '</div>'
            }, options);

            return notifyBase(defaultContent, defaultOptions);
        }

        tools.notifyMini = function (content, options) {
            return notifyMiniBase(content, options);
        };

        tools.loading = function (content, options) {
            return notifyMiniBase(
                $.extend(true, {
                    icon: '/assets/img/loading_3.gif',
                    message: '<span>加载中...</span>'
                }, content),
                $.extend(true, {
                    delay: 0,
                    type: "loading",
                    icon_type: 'image'
                }, options)
            );
        };

        /*
         Dialog
         */
        tools.DEFAULT_TEXTS = {};
        tools.DEFAULT_TEXTS.MESSAGE_TITLE = '消息';
        tools.DEFAULT_TEXTS.CONFIRM_TITLE = '确认消息';
        tools.DEFAULT_TEXTS.OK = '确定';
        tools.DEFAULT_TEXTS.NO = '否定';
        tools.DEFAULT_TEXTS.CANCEL = '取消';
        tools.DEFAULT_TEXTS.CLOSE = '关闭';

        tools.DEFAULT_BUTTONS = {};
        tools.DEFAULT_BUTTONS.COLSE = {
            label: tools.DEFAULT_TEXTS.CLOSE,
            action: function (dialogItself) {
                dialogItself.close();
            }
        };
        tools.DEFAULT_BUTTONS.OK = {
            label: tools.DEFAULT_TEXTS.OK,
            cssClass: 'btn-primary',//dialog.TYPE_PRIMARY,
            action: function (dialogItself) {
                dialogItself.close();
            }
        };

        // 由于原始插件cancel按钮在OK按钮之前，所以直接拷贝原始代码进行微调。Start
        function confirmBaseDialog(arguments) {
            var confirmOptions = {};
            var defaultConfirmOptions = {
                type: dialog.TYPE_PRIMARY,
                title: tools.DEFAULT_TEXTS.CONFIRM_TITLE,//null,
                size: dialog.SIZE_SMALL,//  换成小的.
                message: null,
                closable: false,
                draggable: false,
                btnCancelLabel: tools.DEFAULT_TEXTS.CANCEL,//dialog.DEFAULT_TEXTS.CANCEL,
                btnCancelClass: null,
                btnOKLabel: tools.DEFAULT_TEXTS.OK,//dialog.DEFAULT_TEXTS.OK,
                btnOKClass: null,
                btnOkAutospin: true,
                callback: null,
                callbackContent: null// 扩展。 Extend
            };
            if (typeof arguments[0] === 'object' && arguments[0].constructor === {}.constructor) {
                confirmOptions = $.extend(true, defaultConfirmOptions, arguments[0]);
            } else {
                confirmOptions = $.extend(true, defaultConfirmOptions, {
                    message: arguments[0],
                    callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
                });
            }
            if (confirmOptions.btnOKClass === null) {
                confirmOptions.btnOKClass = ['btn', confirmOptions.type.split('-')[1]].join('-');
            }

            var newDialog = new dialog(confirmOptions);
            newDialog.setData('callback', confirmOptions.callback);
            newDialog.addButton({
                autospin: confirmOptions.btnOkAutospin,
                label: confirmOptions.btnOKLabel,
                cssClass: confirmOptions.btnOKClass,
                action: function (dialogRef) {
                    // Extend
                    if (!isEmpty(confirmOptions.callbackContent)) {
                        dialogRef.getModalBody().html(confirmOptions.callbackContent);
                    }
                    // Extend end.

                    if (typeof dialogRef.getData('callback') === 'function' && dialogRef.getData('callback').call(this, true, dialogRef) === false) {
                        return false;
                    }

                    return dialogRef.close();
                }
            });
            newDialog.addButton({
                label: confirmOptions.btnCancelLabel,
                cssClass: confirmOptions.btnCancelClass,
                action: function (dialogRef) {
                    if (typeof dialogRef.getData('callback') === 'function' && dialogRef.getData('callback').call(this, false, dialogRef) === false) {
                        return false;
                    }

                    return dialogRef.close();
                }
            });
            return newDialog;
        }

        function confirmBase() {
            var newDialog = confirmBaseDialog(arguments);

            return newDialog.open();
        }

        // End

        function threeButtonsModelBase() {
            var defaultConfirmOptions = {
                btnNoLabel: tools.DEFAULT_TEXTS.NO,
                btnNoClass: null,
                size: dialog.SIZE_NORMAL
            };
            var options = extendOptions(defaultConfirmOptions, arguments);
            var newDialog = confirmBaseDialog(options);

            newDialog.addButton({
                label: options.btnNoLabel,
                cssClass: options.btnNoClass,
                action: function (dialogRef) {
                    // Extend
                    if (!isEmpty(options.callbackContent)) {
                        dialogRef.getModalBody().html(options.callbackContent);
                    }
                    // Extend end.

                    if (typeof dialogRef.getData('callback') === 'function' && dialogRef.getData('callback').call(this, 'no', dialogRef) === false) {
                        return false;
                    }

                    return dialogRef.close();
                }
            });
            newDialog.options.buttons[0].action = function (dialogRef) {
                // Extend
                if (!isEmpty(options.callbackContent)) {
                    dialogRef.getModalBody().html(options.callbackContent);
                }
                // Extend end.

                if (typeof dialogRef.getData('callback') === 'function' && dialogRef.getData('callback').call(this, 'ok', dialogRef) === false) {
                    return false;
                }

                return dialogRef.close();
            };
            newDialog.options.buttons[1].action = function (dialogRef) {
                if (typeof dialogRef.getData('callback') === 'function' && dialogRef.getData('callback').call(this, 'cancel', dialogRef) === false) {
                    return false;
                }

                return dialogRef.close();
            };

            return newDialog.open();
        }

        function messageBase() {
            var defaultOptions = {
                title: tools.DEFAULT_TEXTS.MESSAGE_TITLE,
                size: dialog.SIZE_SMALL,
                buttons: [tools.DEFAULT_BUTTONS.COLSE]
            };

            return dialog.show(extendOptions(arguments, defaultOptions));
        }

        function modalBase() {
            var defaultOptions = {
                title: tools.DEFAULT_TEXTS.MESSAGE_TITLE,
                buttons: [{
                    label: tools.DEFAULT_TEXTS.CLOSE,
                    action: function (dialogItself) {
                        dialogItself.close();
                    }
                }]
            };

            return dialog.show(extendOptions(arguments, defaultOptions));
        }

        tools.confirmWaring = function () {
            return confirmBase(extendOptions(arguments, {
                type: dialog.TYPE_WARNING// <-- Default value is BootstrapDialog.TYPE_PRIMARY
            }));
        };

        tools.confirm = function () {
            return confirmBase(extendOptions(arguments, {}));
        };

        tools.message = function () {
            return messageBase(extendOptions(arguments, {}));
        };

        tools.modal = function () {
            return modalBase(extendOptions(arguments, {}));
        };

        tools.modalSave = function () {
            return confirmBase(extendOptions(arguments, {
                title: tools.DEFAULT_TEXTS.MESSAGE_TITLE,
                size: dialog.SIZE_NORMAL,
                closable: true,
                btnOkAutospin: false
            }));
        };

        tools.threeButtonsModel = function () {
            return threeButtonsModelBase(arguments);
        };

        return tools;
    }
);